package classfile

/*
*
方法的行号信息
*/
type LineNumberTableAttr struct {
	AttrStruct
	lineTableLength uint16
	lineNumbers     []LineNumber
}

type LineNumber struct {
	startPc uint16
	lineNo  uint16
}

func (self *LineNumberTableAttr) readAttr(cp ConstantPool) AttributeInfo {
	self.lineTableLength = self.attributeData.ReadUInt16()
	if self.lineTableLength > 0 {
		self.lineNumbers = make([]LineNumber, self.lineTableLength)
		for i := 0; i < int(self.lineTableLength); i++ {
			self.lineNumbers[i] = NewLineNumber(&self.attributeData)
		}
	}
	return self
}

func (self *LineNumberTableAttr) GetLineNumber(pc int) int {
	for i := len(self.lineNumbers) - 1; i >= 0; i-- {
		entry := self.lineNumbers[i]
		if pc >= int(entry.startPc) {
			return int(entry.lineNo)
		}
	}
	return -1

}

func NewLineNumber(data *AttributeData) LineNumber {
	startPc := data.ReadUInt16()
	lineNo := data.ReadUInt16()
	return LineNumber{startPc, lineNo}
}
